#include "Form.h"
#include "login.h"
#include "mainui.h"
#include "ui_login.h"
#include <QGraphicsDropShadowEffect>
#include <qfile.h>
#include <qmessagebox.h>
#include <qtextcodec.h>

login::login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);
    //设置窗体透明
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    //设置无边框
    this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    //实例阴影shadow
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    //设置阴影距离
    shadow->setOffset(-7, 5);
    //设置阴影颜色
    shadow->setColor(QColor("#888888"));
    //设置阴影圆角
    shadow->setBlurRadius(30);
    //给嵌套QWidget设置阴影
    ui->label->setGraphicsEffect(shadow);
    //给垂直布局器设置边距(此步很重要, 设置宽度为阴影的宽度)
    ui->label->setContentsMargins(1,1,1,1);


}

login::~login()
{
    delete ui;
}

void login::on_close_button_clicked()
{
    close();
}

void login::on_register_button_clicked()
{
    Form *f = new Form();
    f->show();
    close();
}

void login::on_sign_button_clicked()
{
    QTextCodec *codec = QTextCodec::codecForName("GB2312");
    QString username = ui->lineEdit_username->text();
    User1::instance()->setUserName(username);
    qDebug() << "username: " << username;
    QString password = ui->lineEdit_passward->text();
    qDebug() << "password: " << password;
    QFile file(QApplication::applicationDirPath() + "/users.txt");
    if(!file.open(QIODevice::ReadWrite|QIODevice::Text)){
        qDebug()<<"文件打开失败";
    }
    file.readLine();//第一行是用户数量，这里用不到
    while(!file.atEnd()){
        QString line = codec->toUnicode(file.readLine()).trimmed();
        QStringList linelist = line.split(" ");
        QString username1 = linelist.at(0);
        qDebug() << "username1: " << username1;
        QString password1 = linelist.at(1);
        qDebug() << "password1: " << username;
        if(username == username1 && password == password1){
//            username = username1;
            User1::instance()->globalManager.readUser();
            int i = 0;
            for(auto each:User1::instance()->globalManager.users){
                if(username.toStdString() == each.username){
                    User1::instance()->setUserIndex(i);
                }
                i++;
            }
            mainUI *w = new mainUI();
            w->show();

            this->close();
            return;
        }

    }
    QMessageBox::StandardButton result=QMessageBox::critical(this, "Title", "密码错误或者用户名不存在");
    file.close();

}

void login::mousePressEvent(QMouseEvent *event)
{

     if( (event->button() == Qt::LeftButton) ){
         mouse_press = true;
         mousePoint = event->globalPos() - this->pos();
 //        event->accept();
    }
     else if(event->button() == Qt::RightButton){
         //如果是右键
        this->close();
     }
}
void login::mouseMoveEvent(QMouseEvent *event)
{


 //    if(event->buttons() == Qt::LeftButton){  //如果这里写这行代码，拖动会有点问题
     if(mouse_press){
         move(event->globalPos() - mousePoint);
 //        event->accept();
     }
}
void login::mouseReleaseEvent(QMouseEvent *event)
{
     mouse_press = false;
}
